import pygame, sys,os
from settingsc import *
from gamedatac import level_0
from levelc import Level
from UI import UI

# function for outputting text onto the screen
def draw_text(text,font,text_col,x,y):
    img = font.render(text, True, text_col) 
    screen.blit(img,(x,y))

class Game:  #can stock constant information
    def __init__(self):
        
        # game attributes
        self.max_level = 0
        self.max_health = 100
        self.cur_health = 100
        self.coins = 0

        # user interface
        self.ui = UI(screen)
        # level setup
        self.level = Level(screen, level_0, self.change_coins,self.change_health)

    def change_coins(self,amount):
        self.coins += amount

    def change_health(self,amount):
        self.cur_health += amount   

    def check_game_over(self):
        if self.cur_health <= 0:
            self.coins = 0
            self.level.status = -1

    def run(self):
        self.level.run()
        self.ui.show_health(self.cur_health,self.max_health)
        self.ui.show_coins(self.coins)
        self.check_game_over()
    
#set up
pygame.init()
screen = pygame.display.set_mode((screen_width,screen_height))
clock = pygame.time.Clock()
game = Game()

font_small = pygame.font.SysFont('Lucida Sans', 15)
font_big = pygame.font.SysFont('Lucida Sans', 40)
fade_counter = 0

while True:
    def ending():
        global fade_counter
        # lose screen
        if (game.level.status == -1) or ((game.level.status == 1) and (game.coins < 0)):
            if fade_counter < screen_width / 2:
                fade_counter += 10
                pygame.draw.rect(screen, 'black', (0, 0, fade_counter, screen_height))
                pygame.draw.rect(screen, 'black', (screen_width - fade_counter, 0, screen_width, screen_height))
            else: 
                screen.fill('black') # fill the screen with black or the after the animation ending the screen will be as before
                draw_text('TRY AGAIN!',font_big,'white',470,250)
                draw_text('SCORE:' + str(game.coins),font_big,'white',500,300)
                # exit
                draw_text('PRESS SPACE TO EXIT',font_big,'white',390,350)
                # play again
                draw_text('PRESS ENTER TO PLAY Again',font_big,'white',320,400)
                key = pygame.key.get_pressed()
                if key[pygame.K_SPACE]:
                    pygame.quit()
                    sys.exit()
                elif key[pygame.K_RETURN]:
                    pygame.quit()
                    os.system('python main.py')
                    quit()

        # win screen
        elif  (game.level.status == 1) and (game.coins >= 0):   
            if fade_counter < screen_width / 2:
                fade_counter += 10
                pygame.draw.rect(screen, 'black', (0, 0, fade_counter, screen_height))
                pygame.draw.rect(screen, 'black', (screen_width - fade_counter, 0, screen_width, screen_height))
            else: 
                screen.fill('black')
                draw_text('YOU WIN!',font_big,'yellow',500,250)
                draw_text('SCORE:' + str(game.coins),font_big,'white',500,300)
                draw_text('PRESS SPACE TO EXIT',font_big,'white',390,350)
                draw_text('Press ENTER TO THE NEXT LEVEL',font_big,'white',320,400)
                key = pygame.key.get_pressed()
                if key[pygame.K_SPACE]:
                    pygame.quit()
                    sys.exit()
                elif key[pygame.K_RETURN]:
                    pygame.quit()
                    os.system('cd ../../level_6/mainpy & python main.py')
                    quit()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill('#573434')
    game.run()
    ending()

    pygame.display.update()
    clock.tick(60)